<!DOCTYPE html>
<html lang="en">

<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <meta name="viewport"
        content="width=device-width, initial-scale=1.0 ,minimum-scale=1.0, maximum-scale=1.0, user-scalable=no" />
    <title>小鹅拼拼 - 支付宝支付</title>
    <link rel="shortcut icon" href="img/xiaoe.ico" />
    <link rel="stylesheet" href="css/wxpay.css" />

    <script src="AmazeUI-2.4.2/assets/js/jquery.min.js"></script>
<!--    <script src="https://cdn.jsdelivr.net/npm/vue@2.6.9/dist/vue.js"></script>-->
<!--    <script src="https://unpkg.com/axios/dist/axios.min.js"></script>-->
    <script src="js/vue.js"></script>
    <script src="js/axios.min.js"></script>
</head>

<body class="body">
    <div id="wxpay" class="container">
        <div class="hd">
            <div class="hd-main">
                <div class="ep-hd-info">
                    <div class="ep-logo">
                        <img src="images/logobig.png" alt="就购物 - 小鹅拼拼" />
                    </div>
                    <div class="ep-order-status">
                        <!-- <h1>订单支付</h1> -->
                        <span class="page-title">订单支付</span>
                    </div>
                </div>
                <div class="user-info">
                    <p>账号：{{ userInfo.nickname }}</p>
                </div>
            </div>
        </div>
        <div class="bd">
            <div class="bd-main">
                <div class="ep-wrapper">
                    <div class="ep-pay-step ep-step-channel bd-main-container" style="padding: 30px 270px 30px 250px;display: block;">
                        <div class="ep-order-detail">
                            <div>
                                <div class="ep-order-tit">
                                    <dl>
                                        <dt>商品订单：</dt>
                                        <dd>{{orderId}}</dd>
                                    </dl>
                                </div>
                                <div class="ep-order-tit">
                                    <span>支付金额：<em class="rmb"><i>¥</i>{{amount / 100}}</em></span>
                                </div>
                            </div>
                            <div>
                                <img src="img/loading.gif" style="height:90px;" alt="正在加载。。。"/>
                                <span class="hit-words">订单[<em style="color: #0ac265">{{orderId}}</em>]正在支付中，请耐心等待...</span>
                            </div>
                            <div class="form" v-html="alipayForm"></div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
        <div class="ft">
            <div class="ft-main">
                <div class="copyright">
                    <p>
                        © 2021 pinpin.com 粤ICP备13009803号-11 深圳小鹅文化传媒有限公司
                        版权所有
                    </p>
                    <p>本服务由鹏城新网 - 小鹅拼拼提供</p>
                </div>
            </div>
        </div>
    </div>
    <script type="text/javascript " src="js/app.js"></script>
<!--    <script src="js/jquery.qrcode.min.js" type="text/javascript"></script>-->
    <script type="text/javascript">
        let index = new Vue({
            el: "#wxpay",
            data: {
                userIsLogin: false,
                userInfo: {},
                orderId: null,
                paymentInfo: [],
                amount: 0,
                alipayForm: "",
            },
            created() {
                let me = this;
                // 通过cookie判断用户是否登录
                me.judgeUserLoginStatus();
                // 获得订单号
                let orderId = app.getUrlParam("orderId");
                // 如果orderId为空，跳转到错误页面
                if (orderId === null || orderId === undefined || orderId === '') {
                    app.goErrorPage();
                    return;
                }
                me.amount = app.getUrlParam("amount");
                me.orderId = orderId;
                me.getAliFrom(orderId);
                // 每个3秒调用后台方法，查看订单是否已经支付成功
                me.setTimer();
            },
            methods: {
                setTimer() {
                    return setInterval(()=>{
                        this.checkPayResult()
                    }, 3000)
                },
                // 查询是否支付成功
                checkPayResult() {
                    let orderId = this.orderId;
                    // console.log(orderId);
                    // 查询订单是否成功
                    let userInfo = this.userInfo;

                    // 发起请求获得微信支付扫描二维码
                    let serverUrl = app.serverUrl;
					axios.defaults.withCredentials = true;
					axios.post(
                            serverUrl + '/orders/getPaidOrderInfo?orderId=' + orderId,
							{},
							{
								headers: {
									'headerUserId': userInfo.id,
									'headerUserToken': userInfo.userUniqueToken
								}
							})
							.then(res => {
								if (res.data.status === 200) {
                                    let orderStatus = res.data.data;
                                    console.log(orderStatus);

                                    if (orderStatus.orderStatus === 20) {
                                        window.location.href = "success.html?orderId=" + orderId + "&orderAmount=" + this.amount;
                                    }
								} else {
									alert(res.data.msg);
								}
							});
                },
                // 获得支付宝构建的支付提交form
                getAliFrom(orderId) {
                    let userInfo = this.userInfo;

                    // 发起请求获得微信支付扫描二维码
                    let paymentServerUrl = app.paymentServerUrl;
                    axios.defaults.withCredentials = true;
                    axios.post(
                        paymentServerUrl + '/payment/goAlipay?merchantUserId=' + userInfo.id + "&merchantOrderId=" + orderId,
                        {},
                        {
                            headers: {
                                'imoocUserId': "imooc",
                                'password': "imooc"
                            }
                        })
                        .then(res => {
                            if (res.data.status === 200) {
                                this.alipayForm = res.data.data;
                                this.$nextTick(()=>{
                                    console.log(document.forms[0]);
                                    document.forms[0].submit();
                                })
                            } else {
                                alert(res.data.msg);
                            }
                        });
                },
                // 通过cookie判断用户是否登录
                judgeUserLoginStatus() {
                    let userCookie = app.getCookie("user");
                    if (
                        userCookie !== null &&
                        userCookie !== undefined &&
                        userCookie !== ""
                    ) {
                        let userInfoStr = decodeURIComponent(userCookie);
                        // console.log(userInfo);
                        if (
                            userInfoStr !== null &&
                            userInfoStr !== undefined &&
                            userInfoStr !== ""
                        ) {
                            let userInfo = JSON.parse(userInfoStr);
                            // 判断是否是一个对象
                            if ( typeof(userInfo)  == "object" ) {
                                this.userIsLogin = true;
                                // console.log(userInfo);
                                this.userInfo = userInfo;
                            } else {
                                this.userIsLogin = false;
                                this.userInfo = {};
                            }
                        }
                    } else {
                        this.userIsLogin = false;
                        this.userInfo = {};
                    }
                }
            }
        });
    </script>
</body>

</html>
